﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using WP.Commons;
using WP.DB;
using WP.Modules;
using WP.Controls;
using WP.Controls.GridColumn;


using WP.RM.BusinessRule;
using WP.RM.BusinessLogical;
using WP.RM.DataAccess;

namespace WP.RM.Form
{
    public partial class frmRMProductBOMCopy : BaseQryRpt 
    {
        private string mstrSourceCategroyID = "";
        private string mstrSourceSetID = "";
        private string mstrSourceProductFItemID = "";

        private string mstrSourceCategroy = "";
        private string mstrSourceSet = "";
        private string mstrSourceProductName = "";
        private string mstrSourceTechnicsName = "";
        private bool mblnSuccess = false;

        public frmRMProductBOMCopy(string strSourceCategory, string strSourceSet, string strSoureceProductFItemID, string strSourceTechnicsName)
        {
            InitializeComponent();
            LoadAllControl(new ArrayList(this.Controls));

            mstrSourceCategroyID = strSourceCategory ;
            mstrSourceSetID = strSourceSet ;
            mstrSourceProductFItemID = strSoureceProductFItemID;

            mstrSourceCategroy = DataFunctions.GetFieldValue("rmCategory", "CategoryName", " CategoryID='" + strSourceCategory + "'");
            mstrSourceSet = DataFunctions.GetFieldValue("rmSet", "SetName", " SetID='" + strSourceSet + "'");
            mstrSourceProductName = DataFunctions.GetFieldValue("rmProduct","ProductName","ProductFItemID='"+ strSoureceProductFItemID +"'") ;
            mstrSourceTechnicsName = strSourceTechnicsName; 
        }

        public void CopyTo(ref bool blnSuccess, ref string strDestCategory, ref string strDestSet, ref string strDestProductName, ref string strDestTechnicsName)
        {
            this.Text = "复制   类别:" + mstrSourceCategroy + "  系列:" + mstrSourceSet + "  产成品:" + mstrSourceProductName + "  到......";
            txtCategory.Text = mstrSourceCategroy ;
            txtSet.Text = mstrSourceSet;

            txtCategoryID.Text = mstrSourceCategroyID;
            txtSetID.Text = mstrSourceSetID; 

            txtProductName.Text = "";
            txtTechnicsName.Text = "";
            this.ShowDialog(); 
            strDestCategory = txtCategoryID.Text;
            strDestSet = txtSetID.Text;
            strDestProductName = txtProductFItemID.Text;
            strDestTechnicsName = txtTechnicsName.Text;
            blnSuccess = mblnSuccess;
        }

        private bool VaildCheck( ref string strErrMsg )
        {
            if (txtProductName.Text == "")
            {
                strErrMsg = "产成品名称不能为空。";
                return false;
            }
            if (DataFunctions.CheckRecordExist("rmProductBOMHdr", txtProductName.Text, "ProductName","TechnicsName='"+ txtTechnicsName.Text +"' and ProductName='"+ txtProductName.Text  +"'"))
            {
                strErrMsg = "BOM已经存在。";
                return false;
            }
            if (!DataFunctions.CheckRecordExist("rmProduct", txtProductName.Text, "ProductName"))
            {
                strErrMsg = "产成品名称不存在。";
                return false;
            }
            if ( mstrSourceProductName  == txtProductName.Text && mstrSourceTechnicsName == txtTechnicsName.Text  )
            {
                strErrMsg = "不能复制，目标和源相同。";
                return false;
            }
            if (txtCategory.Text == "")
            {
                strErrMsg = "类别不能为空。";
                return false;
            }
            if (!DataFunctions.CheckRecordExist("rmCategory", txtCategory.Text, "CategoryName")) 
            {
                 strErrMsg = "类别不存在。";
                return false;
            }
            if (txtSet.Text == "")
            {
                strErrMsg = "系列不能为空";
                return false;
            }
            if ( !DataFunctions.CheckRecordExist ("rmSet",txtSet.Text , "SetName"))
            {
                 strErrMsg = "类别不存在。";
                return false;
            }
            return true;
        }

        protected override void ubtLookUp_Click(object sender, EventArgs e)
        {
            string strFindValue = string.Empty;
            string strFieldName = string.Empty;
            string strSql = string.Empty;
            string strCaption = string.Empty;
            Control con = sender as Control;
            if (con == null) return;
            Control ocon = null;
            switch (con.Name)
            {
                case "ubtCategory":
                    strFindValue = txtCategory.Text;
                    strFieldName = "CategoryName";
                    strSql = BaseFunctions.GetLookUpSQL("rmCategory");
                    ocon = txtCategory;
                    break;

                case "ubtSet":
                    strFindValue = txtSet.Text;
                    strFieldName = "SetName";
                    strSql = BaseFunctions.GetLookUpSQL("rmSet");
                    ocon = txtSet;
                    break;

                case "ubtProductName" :
                    strFindValue = txtProductName.Text;
                    strFieldName = "ProductName";
                    strSql = BaseFunctions.GetLookUpSQL("rmProduct");
                    ocon = txtProductName ;
                    break;
            }
            if (mblnLookUpSortByDescription) strSql = DataFunctions.GetSqlByDescriptionSort(strSql);
            mblnLookUpSortByDescription = false;

            frmLookUp frmlookup = new frmLookUp(strSql, strCaption, strFindValue, strFieldName);
            frmlookup.ShowDialog();
            if (frmlookup.HaveReturnValue)
            {
                ocon.Text = frmlookup.GetReturnValue(strFieldName);
                if (con.Name == ubtCategory.Name)
                {
                    txtCategoryID.Text = frmlookup.GetReturnValue("CategoryID");
                }
                else if (con.Name == ubtSet.Name)
                {                  
                    txtSetID.Text = frmlookup.GetReturnValue("SetID");
                }
                else if (con.Name == ubtProductName.Name)
                {
                    txtProductFItemID.Text = frmlookup.GetReturnValue("ProductFItemID");
                    txtCategoryID.Text = frmlookup.GetReturnValue("CategoryID");
                    txtSetID.Text = frmlookup.GetReturnValue("SetID");

                    txtCategory.Text = frmlookup.GetReturnValue("CategoryName");
                    txtSet.Text = frmlookup.GetReturnValue("SetName");
                }
                ocon.Focus();
            }
            frmlookup.Close();

        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            mblnSuccess = false;
            this.Hide();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            string strErrMsg = "";
            if (!VaildCheck(ref strErrMsg))
            {
                MsgBoxEx.Show(strErrMsg);
                return;
            }
            mblnSuccess = true;
            this.Hide();
        }
    }
}
